use matrix_sdk::{room::Room, ruma::events::room::member::StrippedRoomMemberEvent, Client};
use tokio::time::{sleep, Duration};
pub async fn join(_room_member: StrippedRoomMemberEvent, _client: Client, room: Room) {
    // if room_member.state_key != client.user_id().unwrap() {
    //     return;
    // }
    //if Room::

    if let Room::Invited(room) = room {
        tokio::spawn(async move {
            println!("Autojoining room {}", room.room_id());
            let mut delay = 2;

            while let Err(err) = room.accept_invitation().await {
                // retry autojoin due to synapse sending invites, before the
                // invited user can join for more information see
                // https://github.com/matrix-org/synapse/issues/4345
                eprintln!(
                    "Failed to join room {} ({err:?}), retrying in {delay}s",
                    room.room_id()
                );

                sleep(Duration::from_secs(delay)).await;
                delay *= 2;

                if delay > 3600 {
                    eprintln!("Can't join room {} ({err:?})", room.room_id());
                    break;
                }
            }
            println!("Successfully joined room {}", room.room_id());
        });
    }
}
